﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Month
{
    public class GroupMonthXingNeng
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        public string QueryDate { get; set; }

        public Models.Sys.Groups Group { get; set; }

        public string QueryMonth { get; set; }

        public string QueryYear { get; set; }

        public List<Models.View.OperatorMonthSum> OperatorMonthSums { get; set; }

        /// <summary>
        /// 本月总值
        /// </summary>
        public Models.Month.GroupMonthSum GroupMonthSum { get; set; }

        /// <summary>
        /// 年均值
        /// </summary>
        public Models.Year.GroupAvgYear GroupAvgYear { get; set; }

        /// <summary>
        /// 小组均值
        /// </summary>
        public Models.Month.GroupAvgMonth GroupAvgMonth { get; set; }

        /// <summary>
        /// 公司均值
        /// </summary>
        public Models.Month.CompanyAvgMonth CompanyAvgMonth { get; set; }

        /// <summary>
        /// 上月总值
        /// </summary>
        public Models.Month.GroupMonthSum PreMonthGroupMonthSum { get; set; }

        /// <summary>
        /// 去年同期
        /// </summary>
        public Models.Month.GroupMonthSum PreYearGroupMonthSum { get; set; }

        public string JS { get; set; }

        #region 定义一些使用到的接口
        private Interface.Sys.IGroups IGroups = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.Day.IGroupDayChange IGroupDayChange = null;
        private Interface.Sys.IOperator IOperator = null;
        private Interface.View.IOperatorMonthSum IOperatorMonthSum = null;
        private Interface.Sys.IDayGroupOperators IDayGroupOperators = null;
        private Interface.Month.IGroupMonthSum IGroupMonthSum = null;
        private Interface.Year.IGroupAvgYear IGroupAvgYear = null;
        private Interface.Month.IGroupAvgMonth IGroupAvgMonth = null;
        private Interface.Month.ICompanyAvgMonth ICompanyAvgMonth = null;
        #endregion

        public GroupMonthXingNeng(int groupId, int queryDate, DB.DBManager db)
        {
            InitInterface(db);

            this.QueryDate = queryDate.ToString();
            this.Group = IGroups.GetGroup(groupId);
            this.QueryMonth = this.QueryDate.Substring(4, 2);
            this.QueryYear = this.QueryDate.Substring(0, 4);
            DateTime nowmonth = Convert.ToDateTime(Convert.ToInt32(this.QueryDate.Substring(0, 4)) + "-" + Convert.ToInt32(this.QueryDate.Substring(4, 2)) + "-01");
            List<Models.Day.GroupDayChange> groupDayChanges = IGroupDayChange.GetGroupDayChange(groupId, Convert.ToInt32(this.QueryMonth), Convert.ToInt32(this.QueryYear));
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.MonthBenefitString(groupDayChanges);

            Models.Sys.TradeHistory th = ITradeHistory.GetMonthLastTradingDay(Convert.ToInt32(this.QueryYear), Convert.ToInt32(this.QueryMonth));

            List<Models.Sys.Operator> operators = IDayGroupOperators.GetGroupOperators(groupId, nowmonth);

            this.OperatorMonthSums = IOperatorMonthSum.GetGroupOperatorDayChange(groupId, Convert.ToInt32(this.QueryMonth), Convert.ToInt32(this.QueryYear));

            this.JS += Benefit.Tools.ChartAtManager.ChartManager.GroupPie(operators, this.OperatorMonthSums);

            this.GroupMonthSum = IGroupMonthSum.GetGroupMonthSum(groupId, nowmonth.Month, nowmonth.Year);

            this.GroupAvgYear = IGroupAvgYear.GetGroupAvgYear(groupId, nowmonth.Year);

            this.GroupAvgMonth = IGroupAvgMonth.GetGroupAvgMonth(groupId, nowmonth.Month, nowmonth.Year);

            this.CompanyAvgMonth = ICompanyAvgMonth.GetCompanyAvgMonth(nowmonth.Month, nowmonth.Year);

            DateTime yestermonth = nowmonth.AddMonths(-1);

            this.PreMonthGroupMonthSum = IGroupMonthSum.GetGroupMonthSum(groupId, yestermonth.Month, yestermonth.Year);

            DateTime yesteryear = nowmonth.AddYears(-1);

            this.PreYearGroupMonthSum = IGroupMonthSum.GetGroupMonthSum(groupId, yesteryear.Month, yesteryear.Year);

            DisposeInterface();
        }

        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            IGroups = new Service.Sys.ServiceGroups(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IGroupDayChange = new Service.Day.ServiceGroupDayChange(db);
            IOperator = new Service.Sys.ServiceOperator(db);
            IOperatorMonthSum = new Service.View.ServiceOperatorMonthSum(db);
            IDayGroupOperators = new Service.Sys.ServiceDayGroupOperators(db);
            IGroupMonthSum = new Service.Month.ServiceGroupMonthSum(db);
            IGroupAvgYear = new Service.Year.ServiceGroupAvgYear(db);
            IGroupAvgMonth = new Service.Month.ServiceGroupAvgMonth(db);
            ICompanyAvgMonth = new Service.Month.ServiceCompanyAvgMonth(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            IGroups = null;
            ITradeHistory = null;
            IGroupDayChange = null;
            IOperator = null;
            IOperatorMonthSum = null;
            IDayGroupOperators = null;
            IGroupMonthSum = null;
            IGroupAvgYear = null;
            IGroupAvgMonth = null;
            ICompanyAvgMonth = null;
            #endregion
        }
    }
}
